package com.itheima.web.controller;


import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class GetUsernameController {

    /**
     * user的数据回显 此处只回显 username
     * @return 封装了 username Result对象
     */
    @RequestMapping("/getUsername")
    public Result getUsername(){
        //SpringSecurity 提供的上下文对象 从中可以获取 user的基本信息
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        String username = user.getUsername();

        System.out.println(username);

        return new Result(true, MessageConstant.LOGIN_SUCCESS,username);
    }
}